package com.sgb.merchant.viewmodel;

import android.app.Application;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.entity.AMapAddressEntity;
import com.sgb.link.entity.merchant.IndustryCategoryEntity;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.sgb.link.entity.merchant.InvestmentEntity;
import com.sgb.link.entity.merchant.InvestmentOptions;
import com.sgb.link.utils.EntityConversion;
import com.sgb.merchant.api.MerchantsNet;
import com.sgb.merchant.databinding.MerchantActivityProjectPublishBinding;
import com.sgb.merchant.view.adapter.PicImageAdapter;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.util.DateUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PublishProjectModel extends BaseViewModel {
    public MutableLiveData<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity>>>>>>>> liveDataCategoryList = new MutableLiveData<>();
    public MutableLiveData<InvestmentOptions> ldOptions=new MutableLiveData<>();
    public MutableLiveData<InvestmentOptions> ldCapacityScaleOptions=new MutableLiveData<>();


    public PublishProjectModel(@NonNull @NotNull Application application) {
        super(application);
    }
    /**
     * 行业一级分类列表
     */
    public void getindustryCategoryList() {
        MerchantsNet.getInstance().getService().getCategoryList().enqueue(new ICallBack<BaseEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity>>>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity<List<IndustryCategoryEntity>>>>>>>> data) {
                if(data.isSuccess()){
                    liveDataCategoryList.postValue(data.getData());
                }
            }
        });

    }
    public void getOptions(){
        MerchantsNet.getInstance().getService().getOptions().enqueue(new ICallBack<BaseEntity<InvestmentOptions>>() {
            @Override
            public void onSuccess(BaseEntity<InvestmentOptions> data) {
                if(data.isSuccess()){
                    ldOptions.postValue(data.getData());
                }
            }
        });
    }
    //获取产能规模类型
    public void getCapacityScale(Map<String,Object> map){
        MerchantsNet.getInstance().getService().getCapacityScaleOptions(map).enqueue(new ICallBack<BaseEntity<InvestmentOptions>>() {
            @Override
            public void onSuccess(BaseEntity<InvestmentOptions> data) {
                if(data.isSuccess()){
                    ldCapacityScaleOptions.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 设置项目信息
     * @param mEntity
     * @param imageAdapter
     * @param mBinding
     */
    public void setInfo(InvestmentDetailEntity mEntity, PicImageAdapter imageAdapter, MerchantActivityProjectPublishBinding mBinding) {
        if (mEntity != null) {
            mBinding.itvName.setContent(mEntity.getItemName());
            mBinding.edtMoney.setText(mEntity.getItemMoney());
            mBinding.itvPhone.setContent(mEntity.getLinkPhone());
            mBinding.itvPerson.setContent(mEntity.getLinkName());
            mBinding.etInfo.setText(mEntity.getItemInfo());
            mBinding.itvType.setContent("行业分类已选择");
            mBinding.itvType.setDeliverLineVG(false);
            mBinding.llTypeContent.setVisibility(View.VISIBLE);
            mBinding.tvContent.setText(mEntity.getIndustryName());
            if (!TextUtils.isEmpty(mEntity.getStageName())) {
                mBinding.itvStage.setContent(mEntity.getStageName());
            }
            if (!TextUtils.isEmpty(mEntity.getCapacityScale())) {
                mBinding.itvSize.setContent(mEntity.getCapacityScale());
            }
            if (mEntity.getStatus().equals("0")) {
                mBinding.itvStatus.setContent("已上架");
            } else {
                mBinding.itvStatus.setContent("已下架");
            }
            if (!TextUtils.isEmpty(mEntity.getLandDiscount())) {
                mBinding.itvLand.setContent(mEntity.getLandDiscount());
            }
            if (!TextUtils.isEmpty(mEntity.getLandDiscountContent())) {
                mBinding.etLand.setEditContent(mEntity.getLandDiscountContent());
            }
            if (!TextUtils.isEmpty(mEntity.getTaxDiscount())) {
                mBinding.itvRevenue.setContent(mEntity.getTaxDiscount());
            }
            if (!TextUtils.isEmpty(mEntity.getTaxDiscountContent())) {
                mBinding.etRevenue.setEditContent(mEntity.getTaxDiscountContent());
            }
            mBinding.itvTime.setContent(DateUtils.stampToDay6(mEntity.getApplyStartTime()));
            mBinding.itvEndTime.setContent(DateUtils.stampToDay6(mEntity.getApplyEndTime()));
            mBinding.itvAddress.setText(mEntity.getProviceName() + mEntity.getCityName() + mEntity.getRegionName() + mEntity.getAddressInfo());
            ArrayList<String> ulrList = new ArrayList<>();
            if (mEntity.getImages() != null) {
                for (InvestmentDetailEntity.ImagesBean url : mEntity.getImages()) {
                    ulrList.add(url.getImageUrl());
                }
            }
            imageAdapter.setNetImgList(ulrList);
        }
    }
    /**
     * 获取地址
     */
    public  AMapAddressEntity get(Intent data) {
        if (data == null) {
            return null;
        }
        return data.getParcelableExtra(RouterKey.DATA);
    }
}
